<?php
/**
 * Created by PhpStorm.
 * User: allen
 * Date: 2019/1/23
 * Time: 17:34
 */
namespace app\common\controller;

use push\GeTui;
use think\Controller;

class GetuiMsg extends Controller {
    /**
     * 发送即时消息
     * @param $uid
     * @param $title
     * @param $content
     * @param $tc_content
     * @param int $id
     * @param $out
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function SendJsMessage($uid,$title,$content,$tc_content,$id,&$out){
        $UserModel = new \app\admin\model\Member();
        $user = $UserModel->where('id','eq',$uid) -> field('client_id')->find();
        $client_id = !empty($user['client_id']) ? $user['client_id'] : '';
        $device_token = '';
        if(empty($client_id)){
            $out = '太长时间没重新登录了';
            return false;
        }
        $usertype = 1;//临时设置
        $GeTui = new GeTui();
        $r = $GeTui->SendOneUser(array('touchuan_content'=>$tc_content,'tz_title'=>$title,'message'=>$title,'tz_content'=>$content,'uid'=>$uid,'is_belled'=>1,'is_vibrationed'=>1,'is_cleared'=>1,'id'=>$id,'usertype'=>$usertype),$client_id,'',5, $o1);
        $out = $o1;
        return $r;
    }

    /**
     * 发送带链接的即时消息
     * @param $uid
     * @param $title
     * @param $content
     * @param $link_url
     * @param $id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function SendJsLinkMessage($uid,$title,$content,$link_url,$id){
        $UserModel = new \app\admin\model\Member();
        $user = $UserModel -> where('id','eq',$uid) -> field('client_id')->find();
        $client_id = !empty($user['client_id']) ? $user['client_id'] : '';
        $device_token = '';
        if(empty($client_id)){
            return false;
        }
        $GeTui = new GeTui();
        $r = $GeTui -> SendOneUser(array('tz_title'=>$title,'tz_content'=>$content,'uid'=>$uid,'is_belled'=>1,'is_vibrationed'=>1,'is_cleared'=>1,'link_url'=>$link_url,'id'=>$id), $client_id,$device_token,2, $out);
        return $r;
    }

    /**
     * 发送带下载的即时消息
     * @param $uid
     * @param $title
     * @param $content
     * @param $download_url
     * @param $id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function SendJsDownloadMessage($uid,$title,$content,$download_url,$id){
        $UserModel = new \app\admin\model\Member();
        $user = $UserModel -> where('id','eq',$uid) -> field('client_id')->find();
        $client_id = !empty($user['client_id']) ? $user['client_id'] : '';
        $device_token = '';
        if(empty($client_id)){
            return false;
        }
        $GeTui = new GeTui();
        $r = $GeTui->SendOneUser(array('tz_title'=>$title,'tz_content'=>$content,'uid'=>$uid,'is_belled'=>1,'is_vibrationed'=>1,'is_cleared'=>1,'download_url'=>$download_url,'id'=>$id), $client_id,$device_token,1, $out);
        return $r;
    }

    /**
     * 发送透传即时消息
     * @param $uid
     * @param $tc_content
     * @param $id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function SendJsTouchuanMessage($uid,$tc_content,$id){
        $UserModel = new Member();
        $user = $UserModel ->where('id','eq',$uid)
            ->field('client_id')
            ->find();
        $client_id = !empty($user['client_id']) ? $user['client_id'] : '';
        $device_token = '';
        if(empty($client_id)){
            return false;
        }
        $GeTui = new GeTui();
        $r = $GeTui->SendOneUser(array('uid'=>$uid,'is_belled'=>1,'is_vibrationed'=>1,'is_cleared'=>1,'tc_content'=>$tc_content,'id'=>$id), $client_id,$device_token,4, $out);
        return $r;
    }

    public function SendJsMessageToListV2($android_cidUidArr,$ios_cidUidArr,$title,$content,$tc_content,$id,&$out){
        $GeTui = new GeTui();
        $GeTui->pushMessageToListV4(array('touchuan_content'=>$tc_content,'tz_title'=>$title,'tz_content'=>$content,'is_belled'=>1,'is_vibrationed'=>1,'is_cleared'=>1,'id'=>$id),$android_cidUidArr,0,$io);
        $GeTui->pushMessageToListV4(array('touchuan_content'=>$tc_content,'tz_title'=>$title,'tz_content'=>$content,'is_belled'=>1,'is_vibrationed'=>1,'is_cleared'=>1,'id'=>$id),$ios_cidUidArr,1,$ro1);
        if($io['result']=='ok'|| $ro1['result'] == 'ok'){
            $out = $io['contentId'].';'.$ro1['contentId'];
            return true;
        }else{
            return false;
        }
    }

    //对指定列表用户群发消息
    public function SendJsMessageToList($cidUidArr,$deviceTokenArr,$title,$content,$tc_content,$id,&$out){
        $GeTui = new GeTui();
        $GeTui->pushMessageToList(array('touchuan_content'=>$tc_content,'tz_title'=>$title,'tz_content'=>$content,'is_belled'=>1,'is_vibrationed'=>1,'is_cleared'=>1,'id'=>$id),$cidUidArr,$deviceTokenArr,3,$o);
        if($o['result']=='ok'){
            $out = $o['contentId'];
            return true;
        }else{
            return false;
        }
    }

    //对指定列表用户群发带链接消息
    public function SendLinkMessageToList($cidUidArr,$deviceTokenArr,$title,$content,$link_url,$id,&$out){
        $GeTui = new GeTui();
        $GeTui->pushMessageToList(array('link_url'=>$link_url,'tz_title'=>$title,'tz_content'=>$content,'is_belled'=>1,'is_vibrationed'=>1,'is_cleared'=>1,'id'=>$id),$cidUidArr,$deviceTokenArr,2,$o);
        if($o['result']=='ok'){
            $out = $o['contentId'];
            return true;
        }else{
            return false;
        }
    }

    //对指定列表用户群发带下载消息
    public function SendDownloadMessageToList($cidUidArr,$deviceTokenArr,$title,$content,$download_url,$id,&$out){
        $GeTui = new GeTui();
        $GeTui -> pushMessageToList(array('download_url'=>$download_url,'tz_title'=>$title,'tz_content'=>$content,'is_belled'=>1,'is_vibrationed'=>1,'is_cleared'=>1,'id'=>$id),$cidUidArr,$deviceTokenArr,1,$o);
        if($o['result']=='ok'){
            $out = $o['contentId'];
            return true;
        }else{
            return false;
        }
    }

    //对指定列表用户群发透传消息
    public function SendTouchuanMessageToList($cidUidArr,$deviceTokenArr,$tc_content,$id,&$out=''){
        $GeTui = new GeTui();
        $GeTui -> pushMessageToList(array('touchuan_content'=>$tc_content,'is_belled'=>1,'is_vibrationed'=>1,'is_cleared'=>1,'id'=>$id),$cidUidArr,$deviceTokenArr,5,$o);
        if($o['result']=='ok'){
            $out = $o['contentId'];
            return true;
        }else{
            return false;
        }
    }

    /**
     * uni-app
     * @param array $cids
     * @param array $payload
     * @param string $start
     * @param string $end
     * @param string $out
     * @return bool
     * @throws \Exception
     */
    public function SendTouchuanMessageToUniAppList($cids=[],$payload=[],$start='',$end='',&$out=''){
        $GeTui = new GeTui();
        $result = $GeTui->pushMessageToUniAppList($payload,$cids,$start,$end,$o);
        $out = $o;
        return $result;
    }


    public function SendJsTcMessageToList($cidUidArr,$title,$content,$tc_content,$id,&$out){
        $GeTui = new GeTui();
        $GeTui -> pushMessageToList(array('touchuan_content'=>$tc_content,'tz_title'=>$title,'tz_content'=>$content,'is_belled'=>1,'is_vibrationed'=>1,'is_cleared'=>1,'id'=>$id),$cidUidArr,[],4,$o);
        if($o['result']=='ok'){
            $out = $o;
            return true;
        }else{
            return false;
        }
    }

    //对所有用户群发消息
    public function SendJsMessageToApp($title,$content,$tc_content,$id,&$o){
        $GeTui = new GeTui();
        $GeTui -> pushMessageToApp(array('touchuan_content'=>$tc_content,'tz_title'=>$title,'tz_content'=>$content,'is_belled'=>1,'is_vibrationed'=>1,'is_cleared'=>1,'id'=>$id),array(),4,$out);
        $GeTui -> pushMessageToApp(array('touchuan_content'=>$tc_content,'tz_title'=>$title,'tz_content'=>$content,'is_belled'=>1,'is_vibrationed'=>1,'is_cleared'=>1,'id'=>$id),array(),3,$out);
        if($out['result']=='ok'){
            $o = $out['contentId'];
            return true;
        }else{
            return false;
        }
    }

    //对所有用户群发链接消息
    public function SendLinkMessageToApp($title,$content,$link_url,$id,&$o){
        $GeTui = new GeTui();
        $GeTui -> pushMessageToApp(array('link_url'=>$link_url,'tz_title'=>$title,'tz_content'=>$content,'is_belled'=>1,'is_vibrationed'=>1,'is_cleared'=>1,'id'=>$id),array(),2,$out);
        if($out['result']=='ok'){
            $o = $out['contentId'];
            return true;
        }else{
            return false;
        }
    }
    //对所有用户群发下载消息
    public function SendDownloadMessageToApp($title,$content,$download_url,$id,&$o){
        $GeTui = new GeTui();
        $GeTui -> pushMessageToApp(array('download_url'=>$download_url,'tz_title'=>$title,'tz_content'=>$content,'is_belled'=>1,'is_vibrationed'=>1,'is_cleared'=>1,'id'=>$id),array(),1,$out);
        if($out['result']=='ok'){
            $o = $out['contentId'];
            return true;
        }else{
            return false;
        }
    }

    //对所有用户群发透传消息
    public function SendTouchuanMessageToApp($message = '',$tc_content,$id, &$o) {
        $GeTui = new GeTui();
        $GeTui->pushMessageToApp(array('message'=>$message, 'touchuan_content' => $tc_content,'is_belled' => 1, 'is_vibrationed' => 1, 'is_cleared' => 1,'id'=>$id), array(),4, $out);
        if ($out['result'] == 'ok') {
            $o = $out['contentId'];
            return true;
        } else {
            return false;
        }
    }

    //IOS用户列表
    public function getIosUserList(&$out){
        $UserModel = new \app\admin\model\Member();
        $list = $UserModel
            -> where('device_token','neq','')
            ->field('device_token')
            ->select();
        $out = array();
        if(!empty($list)){
            foreach($list as $v){
                $out[] = $v['device_token'];
            }
        }
        if(!empty($out)){
            return true;
        }
        return false;
    }

}